#!/bin/bash

readonly OUT_DIR=$1
readonly IMG_NAME=$2
readonly PKGLIST=$(readlink -f $3 2> /dev/null)
readonly THIS_DIR="$(readlink -f \"$(dirname $0)\")"

# if an optional .torrent file is desired, see ./TORRENT_METADATA.sample
[[ -f "$THIS_DIR/TORRENT_METADATA" ]] && source "$THIS_DIR/TORRENT_METADATA"

which mktorrent &> /dev/null && readonly HAS_MKTORRENT=1 || readonly HAS_MKTORRENT=0
which rhash     &> /dev/null && readonly HAS_RHASH=1     || readonly HAS_RHASH=0
[[ -n "${TORRENT_ANNOUNCE_URL}" ]] && readonly SHOULD_MKTORRENT=${HAS_MKTORRENT} || \
                                      readonly SHOULD_MKTORRENT=0

echo "ls $THIS_DIR" ; ls $THIS_DIR
[[ -f "$THIS_DIR/TORRENT_METADATA" ]] && echo "./TORRENT_METADATA exists" || echo "./TORRENT_METADATA not exists"
[[ -n "$TORRENT_ANNOUNCE_URL" ]] && echo "TORRENT_ANNOUNCE_URL exists" || echo "TORRENT_ANNOUNCE_URL not exists" ; echo "TORRENT_ANNOUNCE_URL=$TORRENT_ANNOUNCE_URL"

exit

[[ -n "${ISOS_URL}" ]] && readonly SIG_URL=${ISOS_URL}/$(basename ${OUT_DIR})/${IMG_NAME}.sig
[[ -n "${ISOS_URL}" ]] && readonly SIGURL_MSG="GPG signature is expected to be at: ${SIG_URL}"
readonly ISO_HOST_PATH=$(echo "$(basename ${OUT_DIR})/${IMG_NAME}" | sed 's|\s|%20|')
readonly WEBSEEDS_CSV=$(echo $urls | sed -E "s|([^ ]+)|\1${ISO_HOST_PATH}|g" | tr ' ' ',')

readonly PKGLIST_SUCCESS_MSG="\t=> copied $(basename ${PKGLIST}  2> /dev/null)"
readonly SHA512SUMS_SUCCESS_MSG="\t=> SHA512SUMS appended"
readonly WHIIRLPOOLSUMS_SUCCESS_MSG="\t=> WHIRLPOOLSUMS appended"
readonly MAGNET_SUCCESS_MSG="\t=> ${IMG_NAME}.magnet saved"
readonly TORRENT_SUCCESS_MSG="\t=> ${IMG_NAME}.torrent saved\n${SIGURL_MSG}"


MakeMetadata()
{
echo mkmete

  # copy package lists and create checksums and optional torrent file
  [[ -f "${PKGLIST}"     ]] && cp ${PKGLIST} .            && echo -e ${PKGLIST_SUCCESS_MSG}
  rhash --sha512    "${IMG_NAME}" >> SHA512SUMS           && echo -e ${SHA512SUMS_SUCCESS_MSG}
  rhash --whirlpool "${IMG_NAME}" >> WHIRLPOOLSUMS        && echo -e ${WHIIRLPOOLSUMS_SUCCESS_MSG}
  rhash --magnet    "${IMG_NAME}"  > "${IMG_NAME}.magnet" && echo -e ${MAGNET_SUCCESS_MSG}
  (( ${SHOULD_MKTORRENT} )) && MakeTorrent                && echo -e ${TORRENT_SUCCESS_MSG}

  sort SHA512SUMS    --key=2 --output=SHA512SUMS
  sort WHIRLPOOLSUMS --key=2 --output=WHIRLPOOLSUMS
}

MakeTorrent()
{
echo mktorrent

  # create torrent file
  mktorrent --announce=${TORRENT_ANNOUNCE_URL}                                 \
            --comment="${TORRENT_COMMENT}${SIG_URL}"                           \
            $([[ -n "${WEBSEEDS_CSV}" ]] && echo "--web-seed=${WEBSEEDS_CSV}") \
            --piece-length=20 --no-date --verbose                              \
            "${IMG_NAME}"
}

echo ok

if   ! (( ${HAS_RHASH} ))
then echo "[mkmetadata]: rhash is not installed - metadata files will not be created"
elif [[ -d "${OUT_DIR}" ]] && [[ -f "${OUT_DIR}/${IMG_NAME}" ]]
then echo "[mkmetadata]: preparing metadata"

     pushd ${OUT_DIR} > /dev/null
     MakeMetadata
     popd             > /dev/null
fi
